#include "qwidgetfactory.h"
#include "widgets/qformwidget.h"
#include "xmlobject.h"
#include "public/public.h"
#include "realtimedb.h"
QMap<QString, TagWidgetInfo*> QWidgetFactory::m_metaMap;

QWidgetFactory::QWidgetFactory()
{
}

void QWidgetFactory::registerWidget(QString name, QString svgCode,const QMetaObject *host)
{
    TagWidgetInfo *info = m_metaMap.value(name);
    if(info == NULL) {
        info = new TagWidgetInfo;
    }
    info->m_hostObject = host;
    info->m_name = name;
    info->m_svgCode = svgCode;
    m_metaMap.insert(name, info);
}

/**
 * 这里绘制图元
 * @brief QWidgetFactory::createWidget
 * @param name
 * @return
 */
TagWidget* QWidgetFactory::createWidget(const QString &name)
{
    TagWidget* pWidgetObj = NULL;
    if(name == "form") {
        pWidgetObj = new QFormWidget;
    } else {
        qDebug() << "QWidgetFactory::createWidget:meta size->" << m_metaMap.size();
        qDebug() << "QWidgetFactory::createWidget:meta keys->" << m_metaMap.keys();
        qDebug() << "QWidgetFactory::createWidget:meta values->" << m_metaMap.values();
        qDebug() << "QWidgetFactory::createWidget:name->" << name;
        TagWidgetInfo *info = m_metaMap.value(name);
        qDebug() << "QWidgetFactory::createWidget:info->" << info;
        if(info == NULL) {
            return NULL;
        }
        const QMetaObject* obj = info->m_hostObject;
        qDebug() << "QWidgetFactory::createWidget:obj->" << obj;
        qDebug() << "QWidgetFactory::createWidget:obj->" << obj->className();
        if(obj != NULL) {
            pWidgetObj = (TagWidget*)obj->newInstance();
            pWidgetObj->setProperty("SvgCode",info->m_svgCode);
            //pWidgetObj = info->m_tagWidget;
            qDebug() << "QWidgetFactory::createWidget:pWidgetObj in->" << pWidgetObj;
        }
        qDebug() << "QWidgetFactory::createWidget:pWidgetObj out->" << pWidgetObj;
    }

    return pWidgetObj;
}

QMap<QString, TagWidgetInfo*> QWidgetFactory::getWidgetInfo()
{
    return m_metaMap;
}
/**
 *
 * 问题：这里未同步properties相关内容
 * @brief QWidgetFactory::createWidget
 * @param xml
 * @return
 */
TagWidget* QWidgetFactory::createWidget(XMLObject *xml)
{

    qDebug() << "QWidgetFactory::createWidget:start tagname->" << xml->getTagName();
    qDebug() << "QWidgetFactory::createWidget:start xml->" << xml->getText();

    if(xml == NULL) {
        return NULL;
    }

    TagWidget* pWidgetObj = NULL;

    if(xml->getTagName() == "form") {
        //解析画面控件
        pWidgetObj = new QFormWidget;
        pWidgetObj->setProperty("id", xml->getProperty("id"));
        ILoader *pLoaderObj = dynamic_cast<ILoader *>(pWidgetObj);
        if(pLoaderObj) {
            pLoaderObj->fromObject(xml);
        }
        //解析画面子控件
        QList<XMLObject*> objects = xml->getChildren();
        foreach(XMLObject* obj, objects) {
            qDebug() << "QWidgetFactory::createWidget:object tagname->" << obj->getTagName();
            if(obj->getTagName() == OBJECT_TITLE) {
                qDebug() << "QWidgetFactory::createWidget:object id->" << obj->getProperty("id");
                qDebug() << "QWidgetFactory::createWidget:object type->" << obj->getProperty("type");
                TagWidget* pChildWidgetObj = createWidget(obj->getProperty("type"));
                if(pChildWidgetObj) {
                    pChildWidgetObj->setProperty("id", obj->getProperty("id"));
                    ILoader *pLoaderObj = dynamic_cast<ILoader *>(pChildWidgetObj);
                    if(pLoaderObj) {
                        pLoaderObj->fromObject(obj);
                    }
                    //处理附加属性数据
                    QList<XMLObject*> sObjList = obj->getChildren();
                    foreach(XMLObject* subObj,sObjList){
                        QString objName =subObj->getProperty("name");
                        QString objType = subObj->getProperty("type");
                        QString objValue = subObj->getProperty("value");
                        QString objInitialValue = subObj->getProperty("initialValue");
                        qDebug() << "QWidgetFactory::createWidget:object properties type->" << objType;
                        qDebug() << "QWidgetFactory::createWidget:object properties name->" << objName;
                        qDebug() << "QWidgetFactory::createWidget:object properties value->" << objValue;
                        if((objType.compare("Tag",Qt::CaseInsensitive))==0){
                            VarTag varTag ;
                            varTag.name = objName;
                            varTag.value = objValue;//这个初始化值有问题
                            varTag.varType = objType;
                            varTag.varFlag = objValue;
                            varTag.initialValue = objInitialValue;
                            int vtagId = 0;
                            vtagId = tagId(varTag.varFlag);
                            varTag.rtTagid = QString::number(vtagId);
                            pChildWidgetObj->addTag(varTag);
                            qDebug() << "QWidgetFactory::createWidget:object properties value->" << varTag.value;
                        }else if((objType.compare("Rect",Qt::CaseInsensitive))==0){ //处理位置数据
                            QString s_geo = subObj->getProperty("name");
                            QList<XMLObject*> s_geolist = subObj->getChildren();
                            int x = 0;
                            int y = 0;
                            int width = 0;
                            int height = 0;
                            foreach(XMLObject* geoSub,s_geolist){
                                if(geoSub->getProperty("name")=="x"){
                                    QString x_pro = geoSub->getProperty("value");
                                    x=x_pro.toInt();
                                }
                                if(geoSub->getProperty("name")=="y"){
                                    QString y_pro = geoSub->getProperty("value");
                                    y=y_pro.toInt();
                                }
                                if(geoSub->getProperty("name")=="Width"){
                                    QString w_pro = geoSub->getProperty("value");
                                    width = w_pro.toInt();
                                }
                                if(geoSub->getProperty("name")=="Height"){
                                    QString h_pro = geoSub->getProperty("value");
                                    height = h_pro.toInt();
                                }
                            }
                            QRect gRect(x,y,width,height);
                            pChildWidgetObj->setProperty("geometry",gRect);
                            qDebug() << "QWidgetFactory::createWidget:Rect->" << gRect;
                        }else if((objType.compare("Var",Qt::CaseInsensitive))==0){ //处理位置数据
                            QString s_geo = subObj->getProperty("name");
                            QString dataType;
                            dataType = subObj->getProperty("dataType");
                            QList<XMLObject*> s_varlist = subObj->getChildren();
                            QString varTypeStr;
                            QString varTagStr;
                            QString varValueStr;
                            foreach(XMLObject* varSub,s_varlist){
                                if(varSub->getProperty("name")=="varType"){
                                    varTypeStr = varSub->getProperty("value");
                                }
                                if(varSub->getProperty("name")=="varTag"){
                                    varTagStr = varSub->getProperty("value");
                                }
                                if(varSub->getProperty("name")=="varValue"){
                                    varValueStr = varSub->getProperty("value");
                                }
                            }
                            VarTag varTag ;
                            varTag.name = objName;
                            varTag.value = subObj->getProperty("value");//这个初始化值有问题
                            varTag.varFlag = subObj->getProperty("value");
                            varTag.initialValue = subObj->getProperty("initialValue");
                            if(varTypeStr.toInt()==1){
                                int vtagId = 0;
                                vtagId = tagId(varTagStr);
                                pChildWidgetObj->addTag(varTag);
                                varTag.varType = "Tag";
                                varTag.rtTagid = QString::number(vtagId);
                            }else{
                                varTag.varType = dataType;
                                varTag.value = varValueStr;
                            }
                            pChildWidgetObj->addTag(varTag);
                            qDebug() << "QWidgetFactory::createWidget:object properties value->" << varTag.value;
                        }else{//其它类型属性定义
                            VarTag varTag ;
                            varTag.name = objName;
                            varTag.value = objValue;//这个初始化值有问题
                            varTag.varType = objType;
                            varTag.varFlag = objValue;
                            varTag.initialValue = objInitialValue;
                            pChildWidgetObj->addTag(varTag);
                        }

                    }
                    qDebug() <<  "QWidgetFactory::createWidget:tag size->" << pChildWidgetObj->getTags().size();
                    pChildWidgetObj->setParent(pWidgetObj);
                }
            }
        }
    } else {
        pWidgetObj = createWidget(xml->getProperty("type"));
        if(pWidgetObj) {
            pWidgetObj->setProperty("id", xml->getProperty("id"));
            ILoader *pLoaderObj = dynamic_cast<ILoader *>(pWidgetObj);
            if(pLoaderObj) {
                pLoaderObj->fromObject(xml);
            }
        }
    }
    return pWidgetObj;
}

TagWidgetInfo* QWidgetFactory::getWidgetInfo(const QString &name)
{
    return m_metaMap.value(name);
}
